/**
 * Copyright &copy; 2016 CNJSON All rights reserved.
 */
package com.hongsuan.pplive.modules.live.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cnjson.db.persistence.Page;
import com.cnjson.common.utils.StringUtils;
import com.cnjson.common.web.BaseController;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.cnjson.common.config.Global;
import com.hongsuan.pplive.modules.live.entity.LiveGitConfig;
import com.hongsuan.pplive.modules.live.service.LiveGitConfigService;

/**
 * 单表生成Controller
 * 
 * @author cgli
 * @version 2016-06-06
 */
@Controller
@RequestMapping(value = "${adminPath}/live/liveGitConfig")
public class LiveGitConfigController extends BaseController {

	@Autowired
	private LiveGitConfigService liveGitConfigService;

	@ModelAttribute
	public LiveGitConfig get(@RequestParam(required = false) String id) {
		LiveGitConfig entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = liveGitConfigService.get(id);
		}
		if (entity == null) {
			entity = new LiveGitConfig();
		}
		return entity;
	}

	@RequiresPermissions("live:liveGitConfig:view")
	@RequestMapping(value = { "list", "" })
	public String list(LiveGitConfig liveGitConfig, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<LiveGitConfig> page = liveGitConfigService.findPage(new Page<LiveGitConfig>(request, response),
				liveGitConfig);
		model.addAttribute("page", page);
		return "modules/live/liveGitConfigList";
	}

	@RequestMapping(value = "export")
	public String export(RedirectAttributes redirectAttributes) {
		// model.addAttribute("liveGitConfig", liveGitConfig);

		String path = "/home/cgli/gift.json";
		String json = readFileByLines(path);
		System.out.println(json);
		Gson gson = new Gson();
		JsonParser parser = new JsonParser();
		JsonElement el = parser.parse(json);
		// 把JsonElement对象转换成JsonObject
		JsonObject jsonObj = null;
		if (el.isJsonObject()) {
			jsonObj = el.getAsJsonObject();
			
		}
		// 把JsonElement对象转换成JsonArray
		JsonArray jsonArray = null;
		if (el.isJsonArray()) {
			jsonArray = el.getAsJsonArray();
		}
		// 遍历JsonArray对象
		LiveGitConfig config = null;
		Iterator<?> it = jsonArray.iterator();
		while (it.hasNext()) {
			JsonElement e = (JsonElement) it.next();
			config = gson.fromJson(e, LiveGitConfig.class);
			System.out.println("------------>result name =" + config.getName());
			liveGitConfigService.save(config);
		}
		addMessage(redirectAttributes, "保存单表成功");
		return "modules/live/liveGitConfigList";
	}

	public String readFileByLines(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		StringBuilder sb = new StringBuilder();
		try {
			System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				System.out.println("line " + line + ": " + tempString);
				sb.append(tempString);
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return sb.toString();
	}

	@RequiresPermissions("live:liveGitConfig:view")
	@RequestMapping(value = "form")
	public String form(LiveGitConfig liveGitConfig, Model model) {
		model.addAttribute("liveGitConfig", liveGitConfig);
		return "modules/live/liveGitConfigForm";
	}

	@RequiresPermissions("live:liveGitConfig:edit")
	@RequestMapping(value = "save")
	public String save(LiveGitConfig liveGitConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, liveGitConfig)) {
			return form(liveGitConfig, model);
		}
		liveGitConfigService.save(liveGitConfig);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:" + Global.getAdminPath() + "/live/liveGitConfig/?repage";
	}

	@RequiresPermissions("live:liveGitConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(LiveGitConfig liveGitConfig, RedirectAttributes redirectAttributes) {
		liveGitConfigService.delete(liveGitConfig);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:" + Global.getAdminPath() + "/live/liveGitConfig/?repage";
	}

}